package testa;

import entity.Course;
import entity.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class test2  {
    Session session=null;
    Transaction transaction=null;
    @Before
    public  void init(){
        Configuration confi = new Configuration().configure();
        SessionFactory sessionFactory = confi.buildSessionFactory();
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
    }

    @Test
    public void manyToMany(){
        Course course = new Course();
        course.setCouname("java");
        Course course2 = new Course();
        course2.setCouname("c++");
        session.save(course);
        session.save(course2);
        Student student1 = new Student();
        student1.setStuname("张三");
        Student student2 = new Student();
        student2.setStuname("李四");

        student1.getCourses().add(course);
        student1.getCourses().add(course2);

        student2.getCourses().add(course);
        student2.getCourses().add(course2);
        session.save(student1);
        session.save(student2);
    }

    @After
    public void des(){
        transaction.commit();
        session.close();

    }

}
